#pragma once
#ifndef xXMLParser_h__
#define xXMLParser_h__

#include "./skyTreeNode.h"
namespace Sky
{
	namespace NXMLParser
	{
		class xIDMap : public Sky::Memory
		{
		public:
			TUint   id;
			TChar* str;
		};
		//
		class xAttribute: public Sky::Memory
		{
		public:
			xIDMap* att;
			TChar*  val;
			xAttribute* pNext;
			xAttribute():att(vNull),val(vNull),pNext(vNull){};
		};
		//
		class xElement : public Sky::Memory
		{
		public:
			xIDMap* ele;
			TChar*	val;
			xAttribute* pAttr;
			xAttribute* pLast;
			xElement():val(vNull),pAttr(vNull),ele(vNull),pLast(vNull){};
			TVoid addAttribute(xIDMap* attr);
			TVoid setAttributeValue(TChar* val);
			TVoid delAttribute();
		};
		//
		class xDomTreeNode : public TreeNode
		{
			DECL_RTTI1(xDomTreeNode,TreeNode);
		protected:
			xElement element;
		public:
			virtual TVoid setValue(TChar* val,TInt len){}
			virtual TVoid setName(TChar* name,TInt len){};
			virtual TVoid addAttributeName(TChar* name,TInt len){};
			virtual TVoid addAttributeValue(TChar* val,TInt len){};
		};
		class xDomTree
		{
			xElement mRoot;
		};
		//
		class xDomBuilder
		{
		public:
			virtual TVoid BeginDocument(){};
			virtual TBool BeginElement(){return vTrue;}
			virtual TBool ElementName(TChar* ele,TInt len){return vTrue;};
			virtual TBool AttributeName(TChar* attr,TInt len){return vTrue;};
			virtual TVoid AttributeValue(TChar* val,TInt len){};
			virtual TVoid ElementValue(TChar* val,TInt len){};
			virtual TVoid EndElement(){};
			virtual TVoid EndDocument(){};
		};
		
		class xXMLTinyParser
		{
		private:
			typedef enum _xState_
			{
				STATE_XML_XML,
				STATE_XML_ELEMENT_BEGIN,
				STATE_XML_ELEMENT_NAME,
				STATE_XML_ELEMENT_VALUE,
				STATE_XML_ATTRIBUTE_NAME,
				STATE_XML_ATTRIBUTE_VALUE,
				STATE_XML_ELEMENT_FINISH,
				STATE_XML_XML_FINISH

			}xState;
		private:
			TInt   mLen;
			xState mState;
			TBool findElementName(TChar** pBegin,TChar** pEnd,TInt* len,TInt left);
			TBool findElementValue(TChar** pBegin,TChar** pEnd,TInt* len,TInt left);
			TBool findAttributeName(TChar** pBegin,TChar** pEnd,TInt* len,TInt left);
			TBool findAttributeValue(TChar** pBegin,TChar** pEnd,TInt* len,TInt left);
			TBool findElementFinish(TChar** pBegin,TChar** pEnd,TInt* len,TInt left);
		public:
			TVoid Begin();
			TBool Parse(xDomBuilder* domMaker, TChar* pData, TInt nSize);
			TVoid End();
		};
	}
}
#endif // xXMLParser_h__